﻿using EFGetStarted.Migrations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    public string DbPath { get; }

    public BloggingContext()
    {
        var folder = Environment.SpecialFolder.LocalApplicationData;
        var path = Environment.GetFolderPath(folder);
        DbPath = System.IO.Path.Join(path, "blogging.db");
    }

    //配置SQLLite连接字符串(其实就是提供一个本地文件夹用于放数据)
    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite($"Data Source={DbPath}");
}

//博客：一个博客下有多个文章
public class Blog
{
    //主键
    public int BlogId { get; set; }
    //地址
    public string Url { get; set; }
    //列表，外键，不需要初始化
    public List<Post> Posts { get; } = new();
}

//文章：一个文章只属于一个博客
public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

/*Add - Migration InitialCreate为迁移搭建基架，以便为模型创建一组初始表
Update-Database创建数据库并向其应用新的迁移*/